package jMiiEditor.gui.htmldialog;

import jMiiEditor.i18n.I18nBundle;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

import com.googlecode.cchlib.i18n.AutoI18n;

//VS 4E -- DO NOT REMOVE THIS LINE!
public class HTMLDialog
    extends JDialog
        implements HyperlinkListener
{
    private static final long serialVersionUID = 1L;
    protected JButton jButton_Back;
    protected JButton jButton_Index;
    private   JButton jButton_OK;
    private JPanel jPanelBottom;
    private JEditorPane jEditorPaneHTML;
    private JScrollPane jScrollPaneHTML;
    private URL                 indexUrl;
    private LinkedList<URL>     urls = new LinkedList<URL>();
    private AutoI18n            autoI18n = I18nBundle.getAutoI18n();
    private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
    public HTMLDialog()
    {
        initComponents();
        initFixComponents();
    }

    public HTMLDialog( Frame parent )
    {
        super( parent );
        initComponents();
        initFixComponents();
    }

    private void initComponents() {
        setFont(new Font("Dialog", Font.PLAIN, 12));
        setBackground(Color.white);
        setForeground(Color.black);
        add(getJScrollPaneHTML(), BorderLayout.CENTER);
        add(getJPanelBottom(), BorderLayout.SOUTH);
        setSize(480, 300);
    }

    private void initFixComponents()
    {
        jEditorPaneHTML.setEditable( false );
        jEditorPaneHTML.addHyperlinkListener( this );

        // Apply i18n !
        autoI18n.performeI18n(this,HTMLDialog.class);
    }

    private JButton getJButton_Back() {
        if (jButton_Back == null) {
            jButton_Back = new JButton();
            jButton_Back.setText("Back");
            jButton_Back.addMouseListener(new MouseAdapter() {

                public void mousePressed(MouseEvent event) {
                    jButton_Back_MouseMousePressed(event);
                }
            });
        }
        return jButton_Back;
    }

    private JButton getJButton_Index() {
        if (jButton_Index == null) {
            jButton_Index = new JButton();
            jButton_Index.setText("Index");
            jButton_Index.addMouseListener(new MouseAdapter() {

                public void mousePressed(MouseEvent event) {
                    jButton_Index_MouseMousePressed(event);
                }
            });
        }
        return jButton_Index;
    }

    private JScrollPane getJScrollPaneHTML() {
        if (jScrollPaneHTML == null) {
            jScrollPaneHTML = new JScrollPane();
            jScrollPaneHTML.setViewportView(getJEditorPaneHTML());
        }
        return jScrollPaneHTML;
    }

    private JEditorPane getJEditorPaneHTML() {
        if (jEditorPaneHTML == null) {
            jEditorPaneHTML = new JEditorPane();
            jEditorPaneHTML.setText("jEditorPane0");
        }
        return jEditorPaneHTML;
    }

    private JPanel getJPanelBottom() {
        if (jPanelBottom == null) {
            jPanelBottom = new JPanel();
            jPanelBottom.setLayout(new BorderLayout());
            jPanelBottom.add(getJButton_Back(), BorderLayout.WEST);
            jPanelBottom.add(getJButton_Index(), BorderLayout.CENTER);
            jPanelBottom.add(getJButton_OK(), BorderLayout.EAST);
        }
        return jPanelBottom;
    }

    private JButton getJButton_OK() {
        if (jButton_OK == null) {
            jButton_OK = new JButton();
            jButton_OK.setText("OK");
            jButton_OK.addMouseListener(new MouseAdapter() {

                public void mousePressed(MouseEvent event) {
                    jButton_OK_MouseMousePressed(event);
                }
            });
        }
        return jButton_OK;
    }

    @SuppressWarnings("unused")
    private static void installLnF()
    {
        try {
            String lnfClassname = PREFERRED_LOOK_AND_FEEL;
            if( lnfClassname == null )
                lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
            UIManager.setLookAndFeel( lnfClassname );
        }
        catch( Exception e ) {
            System.err.println( "Cannot install " + PREFERRED_LOOK_AND_FEEL
                    + " on this platform:" + e.getMessage() );
        }
    }

    /**
     * Main entry of the class.
     * Note: This class is only created so that you can easily preview the result at runtime.
     * It is not expected to be managed by the designer.
     * You can modify it as you like.
     * @param args
     */
    public static void main( String[] args )
    {
        installLnF();
        SwingUtilities.invokeLater( new Runnable() {
            @Override
            public void run()
            {
                HTMLDialog dialog = new HTMLDialog();
                dialog.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
                dialog.setTitle( "About" );
                dialog.setLocationRelativeTo( null );
                dialog.getContentPane().setPreferredSize( dialog.getSize() );
                dialog.pack();
                dialog.setVisible( true );
            }
        } );
    }

    @Override
    public void hyperlinkUpdate( HyperlinkEvent e )
    {
        try {

            if( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED ) {
                //Document doc = jEditorPaneHTML.getDocument();
                //System.out.println( e.getURL() );
                setURL( e.getURL() );
                //getToolkit().beep();
                //System.out.println( "Listening" );
            }

        }
        catch( Exception ex ) {
            // html.setDocument(doc);
            ex.printStackTrace();
        }
    }

    private void jButton_OK_MouseMousePressed(MouseEvent event)
    {
        dispose();
    }

    public void setURL(URL anURL) throws IOException
    {
        this.urls.add( anURL );
        this.jEditorPaneHTML.setPage( anURL );
     }

    public void setIndexURL(URL anURL) throws IOException
    {
        this.indexUrl = anURL;
        gotoIndex();
     }

    private void jButton_Back_MouseMousePressed(MouseEvent event)
    {
        try {
            gotoBack();
        }
        catch( IOException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void jButton_Index_MouseMousePressed(MouseEvent event)
    {
        try {
            gotoIndex();
        }
        catch( IOException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void gotoIndex() throws IOException
    {
        setURL( this.indexUrl );
    }

    public void gotoBack() throws IOException
    {
        URL anURL = this.urls.pollLast();

        if( anURL != null ) {
            anURL = this.urls.pollLast();

            if( anURL == null ) {
                getToolkit().beep();
            }
            else {
                setURL( anURL );
            }
        }
        else {
            gotoIndex();
            getToolkit().beep();
        }
    }

}
